<template>
  <div class="main-box">
    <v-header :platform-name="this.platformName" :system-logo-url="this.systemLogoUrl" v-if="!isEworldSatus"/>
    <v-sidebar v-if="!isEworldSatus"/>
    <div class="content-box" :class="{ 'content-collapse': collapse,'active':isEworldHas }" >
      <div class="content" :class="{ 'active':isEworldHas}">
        <router-view />
      </div>
      <!-- <el-footer class="footer">
                <div style="
                        display: flex;
                        justify-content: center;
                        align-items: center;
                    ">
                    <div style="color: #888888; line-height: 48px">
                        © FOREMOST. 上海爱可及医疗科技有限公司 V 1.0
                    </div>
                </div>
      </el-footer>-->
    </div>
  </div>
</template>

<script>
import vHeader from './Header';
import vSidebar from './Sidebar';

export default {
  name: 'LayoutMain',
  components: {
    vHeader,
    vSidebar
  },
  data() {
    return {
      ImageServiceURL: process.env.ImageServiceURL,
      systemLogoUrl: '', // 系统logo地址
      checkTimeoutInterval: null, // 用户操作监听
      platformName: '', // 平台名称
      userData: null, // 用户信息
      isFold: false, // 侧边栏是否折叠
      isEworldHas:this.$store.state.isEworld,
      permissionGroupList: this.$store.state.permissionList // 权限列表数据结构
    };
  },
   computed: {
    // 用户ID
    userID() {
      return this.$store.state.user.id;
    },
    isEworldSatus(){
      return this.$store.state.isEworld;
    },
    // 用户名
    userName() {
      return this.$store.state.user.name;
    },
    // 用户头像图片链接
    userHeadImgUrl() {
      if (this.$store.state.user.headImgUrl) {
        return process.env.ImageServiceURL + this.$store.state.user.headImgUrl;
      }
      return '/images/doctor.png';
    },
    // 侧边栏宽度
    sideWidth() {
      return this.isFold ? '64px' : '180px';
    },
    // 默认激活权限项
    nowRoute() {
      console.log('nowRoute', this.$store.state.nowRoute);
      return this.$store.state.nowRoute;
    },
    collapse() {
      return this.$store.state.collapse;
    }
  },
  mounted() {
    // 是否已登录
    let query = this.$route.query
    console.log(query)
    console.log("alyoutMain")
    if(query && query.from == 'eworld'){//明天医网
     this.$store.commit('setisEworld', {
            key: 'isEworld',
            value: true
          });
     this.isEworldHas=true
    }else{
      if (!this.userID) {
      this.$message.info('请先登录');
      this.logout();
      return;
    }
    }
   
    this.__init();
    this.getPlatform();
  },
  methods: {
    async getPlatform() {
      try {
        await this.$axios({
          // 详见 axios 请求配置项
          method: 'GET',
          url: '/PROXY-Remote/institutions/platform',
          headers: {
            authorization: this.$store.getters.getAuthorization
          }
        }).then(res => {
          // 请求失败
          if (res.data.code !== 200) {
            throw new Error(res.data);
          }
          // 请求成功
          const result = res.data.data;
          this.platformName = result.abbreviation;
          if (!!result.logoUrl) {
            this.systemLogoUrl = this.ImageServiceURL + result.logoUrl;
          }
        });
      } catch (e) {
        // 跳出
      }
    },
    __init() {
      const that = this;
      this.$store.commit('setPropertyGeneral', {
        key: 'lastTime',
        value: `${new Date().getTime()}`
      });

      window.onload = function() {
        window.document.onmousedown = function() {
          that.$store.commit('setPropertyGeneral', {
            key: 'lastTime',
            value: `${new Date().getTime()}`
          });
        };
      };

      // 无操作一段时间后登出
      const checkTimeout = function() {
        const currentTime = new Date().getTime(); // 更新当前时间
        const { lastTime } = that.$store.state;
        const timeOut = 48 * 60 * 60 * 1000; // 超时时间为48h
        console.log('no operation time', currentTime - lastTime);

        if (currentTime - lastTime > timeOut) {
          // 判断是否超时
          if (that.checkTimeoutInterval) {
            clearInterval(that.checkTimeoutInterval);
            that.checkTimeoutInterval = null;
          }
          that.logout();
        }
      };
      //this.checkTimeoutInterval = window.setInterval(checkTimeout, 30000); // 30s 检查一次
    },
    // 登出确认
    logoutConfirm() {
      this.$confirm('即将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.logoutApi();
      });
    },
    logoutApi() {
      this.$axios({
        method: 'PUT',
        url: '/PROXY-Remote/logout',
        headers: {
          authorization: this.$store.getters.getAuthorization
        }
      }).then(res => {
        if (res.data.code === 200) {
          if (res.data.data.isShowFrontPage === 'true') {
            this.$router.push('/newLogin');
          } else {
            this.$router.push('/Login');
          }
          this.logout();
        }
      });
    },
    // 登出操作
    logout() {
      if (this.checkTimeoutInterval) {
        clearInterval(this.checkTimeoutInterval);
        this.checkTimeoutInterval = null;
      }
      this.$store.commit('setPropertyGeneral', {
        key: 'authorization',
        value: ''
      });
      this.$store.commit('setisEworld', {
            key: 'isEworld',
            value: false
          });
          this.isEworldHas=false
      this.$router.push('/Login');
    },
    foldAside() {
      this.isFold = !this.isFold;
    },
    // 跳转个人信息编辑页面
    updatePersonInfo() {
      this.$store.commit('setPropertyGeneral', {
        key: 'userID',
        value: this.userID
      });
      this.$router.push('/editPersonalInfo');
    },
    // 跳转修改密码页面
    updatePassword() {
      this.$router.push('/ChangePassword');
    }
  },

 
  destroyed() {
    if (this.checkTimeoutInterval) {
      clearInterval(this.checkTimeoutInterval);
      this.checkTimeoutInterval = null;
    }
  }
};
</script>

<style scoped>
.main-box::-webkit-scrollbar {
  width: 0;
}
.content::-webkit-scrollbar {
  width: 0;
}
.main-box{
  height:100vh;
  width:100%;
}
.content-box.active{
  left:0px;
  top:0px;
  position: relative;
  width:100%;
  height:calc(100vh - 50px);
}
.content-box {
  position: absolute;
  left: 200px;
  right: 0;
  top: 70px;
  bottom: 0;
  padding-bottom: 5px;
  -webkit-transition: left 0.3s ease-in-out;
  transition: left 0.3s ease-in-out;
  background: #f0f0f0;
  height:calc(100vh - 75px);
}
.content.active{
  width:100%;
  top:0px;
  /* height:100vh; */
  height:100%;
}
.content {
  top: 60px;
  width: auto;
  overflow-y: scroll;
  box-sizing: border-box;
  height:100%;
}

.main .user-info {
  float: right;
}

.logo img {
  vertical-align: middle;
}

svg.awesome {
  height: 18px;
  width: 18px;
  color: #ffffff;
}

.footer {
  -webkit-box-flex: 0;
  -ms-flex: 0 0 auto;
  flex: 0 0 auto;
  background-color: #f2f2f2;
}

.content-collapse {
  left: 65px;
}
</style>
